import { 
  queryUsers, 
  addUser ,
  updateUser,
  changeUserState, 
  getBranchTreeList, 
  changePassword,
  getPostListByBranchId,
  updatePassword,
  existUserName
} from '../services/api';
import { message } from 'antd';
import { routerRedux } from 'dva/router';

export default {
  namespace: 'user1',

  state: {
      modalVisible: false,
      userList: [],
      page:{},
      branchList: [],
      loading: false,
      detailValue: {},
      confirmLoading : false,
      jobsList: [],
      branchTreeList:[],
  },

  effects: {
  
    *fetch({ payload ,callback}, { call, put }) {
      yield put({
        type: 'query',
        payload: {
          loading: true,
        },
      });
      const response = yield call(queryUsers, payload);
      
      if(response.result.code !== '200'){
        message.error(response.result.message)
      }
      yield put({
        type: 'query',
        payload: {
          userList: response.data.list,
          page: response.data.page,
          loading: false,
        },
      });
      if(callback)callback();
    },

    *add({ payload, callback }, { call, put }) {
      // console.log("qqqqqqqqqqqqq")
      // console.log(payload)
      yield put({
        type: 'changeSubmitting',
        payload: true,
      });
        const response = yield call(addUser, payload);
        // console.log("FFFFFFFFF")
        // console.log(response)
        yield put({
          type: 'changeSubmitting',
          payload: false,
        });

        if(response.result.code=="200"){
          message.success('提交成功');
        }else{
          message.error(response.result.message);
        }
  
        yield put({
          type: 'save',
          payload: response,
        });
        if (callback) callback();

    },

    *update({ payload, callback }, { call, put }) {
      // console.log("RRRRRRRR")
      // console.log(payload)
      yield put({
        type: 'changeSubmitting',
        payload: true,
      });
        const response = yield call(updateUser, payload);
        // console.log("DDDDDDDD")
        // console.log(payload)
        yield put({
          type: 'changeSubmitting',
          payload: false,
        });

        if(response.result.code=="200"){
          message.success('提交成功');
        }else{
          message.error(response.result.message);
        }
  
        yield put({
          type: 'save',
          payload: response,
        });
        if (callback) callback();

    },

    *changeUserState({ payload, callback }, { call, put }) {

      const response = yield call(changeUserState, payload);

      if (response.result.code == "200") {
        message.success('改变状态成功');
      } else {
        message.error(response.result.message);
      }
      yield put({
        type: 'save',
        payload: { data: response },
      });
      if (callback) callback();

    },
    *changePassword({ payload, callback }, { call, put }) {
      // console.log("RRRRRRRR")
      // console.log(payload)
      const response = yield call(changePassword, payload);
      // console.log("DDDDDDDD")
      // console.log(response)

      if (response.result.code == "200") {
        message.success('密码重置成功');
      } else {
        message.error(response.result.message);
      }
      yield put({
        type: 'save',
        payload: { data: response },
      });
      if (callback) callback();

    },
    *getBranchTreeList({ payload ,callback}, { call, put }) {
      const response = yield call(getBranchTreeList, payload);
      // console.log("ADDADADDDDDDDDDDD")
      // console.log(response)
      yield put({
        type: 'query',
        payload: {
          branchTreeList: response.data,
        },      
      });
      if(callback)callback();
    },
   
    *getPostListByBranchId({ payload }, { call, put }) {
      // console.log("DDDDDDD")
      // console.log(payload)
      const response = yield call(getPostListByBranchId, payload);
      // console.log("wwwwwwwwwwwwwww")
      // console.log(response.data)
      yield put({
        type: 'query',
        payload: {
          jobsList: response.data,
        },      
      });
    },

    *updatePassword({ payload, callback }, { call, put }) {
      // console.log("DDDDDDD")
      // console.log(payload)
      const response = yield call(updatePassword, payload);
      // console.log("wwwwwwwwwwwwwww")
      // console.log(response)
      if(response.result.code == 200){
        message.success("密码修改成功")
        
      }else{
        message.error(response.result.message)
      }
      yield put({
        type: 'query',
        payload: {
          data: response.data,
        },      
      });
      if(callback)callback()
    },
    *existUserName({ payload, callback }, { call, put }) {   
      
      const response = yield call(existUserName, payload);
      
      if(response.result.code == 200){
      }else{
        message.error(response.result.message)
      }
      yield put({
        type: 'query',
        payload: {
          data: response.data,
        },      
      });
      if(callback)callback()
    },
},

  reducers: {//纯函数， 输入什么输出就是什么
    query(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    showEdit (state, action) {
      return { 
        ...state,
        modalVisible: action.payload,
      };
    },
    setValue (state, action) {
      return { 
        ...state,
        detailValue: action.payload,
      };
    },

    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },

    changeSubmitting(state, { payload }) {
      return {
        ...state,
        confirmLoading: payload,
        modalVisible: !payload && state.modalVisible ?  false : state.modalVisible,
      };
    },

  },
};
